import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:magic_car/UI/Loading.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';

class DrawPage extends StatefulWidget {
  // final String title;
  final String types;
  DrawPage(this.types);

  @override
  _MyHomePageState createState() => new _MyHomePageState(this.types);
}

class _MyHomePageState extends State<DrawPage> {
  String paytype = "0";
  String name = "";
  String acount = "";
  String mobile = "";
  String money = "";
  String banckName = "";
  bool hasAcountInfo = false;
  Map lastAountInfo = new Map();
  TextEditingController banknameController = new TextEditingController();
  TextEditingController acontnameController = new TextEditingController();
  TextEditingController acountController = new TextEditingController();
  TextEditingController mobileController = new TextEditingController();

  final String types;
  _MyHomePageState(this.types);
  initState() {
    super.initState();

    loadacoutinfo();
  }

  //申请提现
  applydraw() async {
    if (acontnameController.text == "" ||
        mobileController.text == "" ||
        acountController.text == "" ||
        banknameController.text == "") {
      Toast.show("请填写完整的提现信息", context);
      return;
    }
    // LoadingPage loadpage = LoadingPage(context);
    // loadpage.show();

    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["id"] = pref.getString('uid');
    map["name"] = acontnameController.text;
    map["mobile"] = mobileController.text;
    map["account"] = acountController.text;
    map['income_type'] = this.types;
    // map["money"] = money;
    map["bankname"] = banknameController.text;
    print(map);

    HttpHelper.post(HttpHelper.API_SubDrawApply, (data) async {
      //loadpage.close();
      if (data != null) {
        print(data);
        
        if (data["code"].toString() == "1") {
          Navigator.pop(context);
          Toast.show(data['info'], context);
        }else{
       Toast.show(data['msg'], context);
        }
        // Toast.show(data['msg'], context);
        // print(data);
        // print(data["info"].length.toString());
        // if (data["code"].toString() == "1") {
        //   setState(() {});
        // }
      }
    }, params: map);
  }

  loadacoutinfo() async {
    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["id"] = pref.getString('uid');
    map["role"] = "2";

    print(map);

    HttpHelper.post(HttpHelper.API_WithdrawAcontInfo, (data) async {
      //loadpage.close();
      if (data != null) {
        print(data);
       // Toast.show(data['msg'], context);
        if (data["code"].toString() == "1") {
          var acountinfo = data['info'];
          setState(() {
            hasAcountInfo = true;
            lastAountInfo = acountinfo;
          });
        }
      }
    }, params: map);
  }

  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('提现'),
          backgroundColor: Colors.white,
          elevation: 0,
        ),
        body: new Container(
          color: Color.fromRGBO(239, 239, 239, 1),
          child: new ListView(
            children: <Widget>[
              new Padding(
                padding: new EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 15.0),
                child: new Column(
                  children: <Widget>[
                    new Container(
                        color: Colors.white,
                        child: new Column(children: <Widget>[
                          // new Padding(
                          //     padding:
                          //         new EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                          //     child: new Container(
                          //       height: 50,
                          //       child: Row(children: <Widget>[
                          //         new Text("提现金额"),
                          //         Expanded(
                          //             child: new TextField(
                          //                 textAlign: TextAlign.right,
                          //                 onChanged: (value) {
                          //                   setState(() {
                          //                     money = value;
                          //                   });
                          //                 },
                          //                 // controller: _validtroller,
                          //                 decoration: InputDecoration(
                          //                   hintText: '单次最少提现100元',
                          //                   border: OutlineInputBorder(
                          //                     borderSide: BorderSide.none,
                          //                   ),
                          //                 )))
                          //       ]),
                          //     )),
                          // new Container(
                          //   height: 1,
                          //   color: Color.fromRGBO(239, 239, 239, 1),
                          // ),

                          new Padding(
                              padding:
                                  new EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                              child: new Container(
                                height: 50,
                                child: Row(children: <Widget>[
                                  new Text("银行"),
                                  Expanded(
                                      child: new TextField(
                                          onChanged: (value) {
                                            setState(() {
                                              banckName = value;
                                            });
                                          },
                                          textAlign: TextAlign.right,
                                          controller: banknameController,
                                          // controller: _validtroller,
                                          decoration: InputDecoration(
                                            hintText: '请填写银行名称',
                                            border: OutlineInputBorder(
                                              borderSide: BorderSide.none,
                                            ),
                                          )))
                                ]),
                              )),
                          new Container(
                            height: 1,
                            color: Color.fromRGBO(239, 239, 239, 1),
                          ),
                          new Padding(
                              padding:
                                  new EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                              child: new Container(
                                height: 50,
                                child: Row(children: <Widget>[
                                  new Text("姓名"),
                                  Expanded(
                                      child: new TextField(
                                          onChanged: (value) {
                                            setState(() {
                                              name = value;
                                            });
                                          },
                                          controller: acontnameController,
                                          textAlign: TextAlign.right,
                                          // controller: _validtroller,
                                          decoration: InputDecoration(
                                            hintText: '请填写您的真实姓名',
                                            border: OutlineInputBorder(
                                              borderSide: BorderSide.none,
                                            ),
                                          )))
                                ]),
                              )),
                          new Container(
                            height: 1,
                            color: Color.fromRGBO(239, 239, 239, 1),
                          ),
                          new Padding(
                              padding:
                                  new EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                              child: new Container(
                                height: 50,
                                child: Row(children: <Widget>[
                                  new Text("账号"),
                                  Expanded(
                                      child: new TextField(
                                          onChanged: (value) {
                                            setState(() {
                                              acount = value;
                                            });
                                          },
                                          controller: acountController,
                                          textAlign: TextAlign.right,
                                          // controller: _validtroller,
                                          decoration: InputDecoration(
                                            hintText: '请填写正确的银行卡号',
                                            border: OutlineInputBorder(
                                              borderSide: BorderSide.none,
                                            ),
                                          )))
                                ]),
                              )),
                          new Container(
                            height: 1,
                            color: Color.fromRGBO(239, 239, 239, 1),
                          ),
                          new Padding(
                              padding:
                                  new EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                              child: new Container(
                                height: 50,
                                child: Row(children: <Widget>[
                                  new Text("联系电话"),
                                  Expanded(
                                      child: new TextField(
                                          onChanged: (value) {
                                            setState(() {
                                              mobile = value;
                                            });
                                          },
                                          textAlign: TextAlign.right,
                                          controller: mobileController,
                                          decoration: InputDecoration(
                                            hintText: '请填写联系电话',
                                            border: OutlineInputBorder(
                                              borderSide: BorderSide.none,
                                            ),
                                          )))
                                ]),
                              )),
                          new Container(
                            height: 1,
                            color: Color.fromRGBO(239, 239, 239, 1),
                          ),
                        ])),
                    // new Padding(
                    //     padding: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                    //     child: Align(
                    //         alignment: Alignment.centerLeft,
                    //         child:
                    //             new Text("单笔支付为100.00-5000.00元，单日提现不超过2000.00元",
                    //                 style: TextStyle(
                    //                   fontSize: 11,
                    //                   color: Color.fromRGBO(
                    //                     153,
                    //                     153,
                    //                     153,
                    //                     1,
                    //                   ),
                    //                 )))),
                    hasAcountInfo
                        ? new Padding(
                            padding:
                                new EdgeInsets.fromLTRB(20.0, 50.0, 20.0, 0.0),
                            child: new Container(
                              width: double.infinity,
                              height: 40,
                              child: new Material(
                                color: Color.fromRGBO(239, 239, 239, 1),
                                //INK可以实现装饰容器
                                child: new Ink(
                                  //用ink圆角矩形
                                  // color: Colors.red,
                                  decoration: new BoxDecoration(
                                    //背景
                                    color: Color.fromRGBO(27, 182, 251, 1),
                                    //设置四周圆角 角度
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(20.0)),
                                    //设置四周边框
                                    border: new Border.all(
                                        width: 1,
                                        color: Color.fromRGBO(27, 182, 251, 1)),
                                  ),
                                  child: new InkWell(
                                      //圆角设置,给水波纹也设置同样的圆角
                                      //如果这里不设置就会出现矩形的水波纹效果
                                      borderRadius:
                                          new BorderRadius.circular(15.0),

                                      //设置点击事件回调
                                      onTap: () {
                                        var acountinfo = lastAountInfo;
                                        banknameController.text =
                                            acountinfo['bankname'];
                                        acontnameController.text =
                                            acountinfo['name'];
                                        acountController.text =
                                            acountinfo['account'];
                                        mobileController.text =
                                            acountinfo['mobile'];
                                        // applydraw();
                                      },
                                      child: Container(
                                        //设置 child 居中
                                        alignment: Alignment(0, 0),
                                        height: 40,
                                        width: double.infinity,
                                        //color: Color.fromRGBO(27, 182, 251, 1),
                                        child: Text(
                                          "使用上次填写的账号信息",
                                          style: TextStyle(color: Colors.white),
                                        ),
                                      )),
                                ),
                              ),
                            ))
                        : new Container(height: 0),
                    new Padding(
                        padding: new EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                        child: new Container(
                          width: double.infinity,
                          height: 40,
                          child: new Material(
                            color: Color.fromRGBO(239, 239, 239, 1),
                            //INK可以实现装饰容器
                            child: new Ink(
                              //用ink圆角矩形
                              // color: Colors.red,
                              decoration: new BoxDecoration(
                                //背景
                                color: Color.fromRGBO(27, 182, 251, 1),
                                //设置四周圆角 角度
                                borderRadius:
                                    BorderRadius.all(Radius.circular(20.0)),
                                //设置四周边框
                                border: new Border.all(
                                    width: 1,
                                    color: Color.fromRGBO(27, 182, 251, 1)),
                              ),
                              child: new InkWell(
                                  //圆角设置,给水波纹也设置同样的圆角
                                  //如果这里不设置就会出现矩形的水波纹效果
                                  borderRadius: new BorderRadius.circular(15.0),

                                  //设置点击事件回调
                                  onTap: () {
                                    applydraw();
                                  },
                                  child: Container(
                                    //设置 child 居中
                                    alignment: Alignment(0, 0),
                                    height: 40,
                                    width: double.infinity,
                                    //color: Color.fromRGBO(27, 182, 251, 1),
                                    child: Text(
                                      "提现",
                                      style: TextStyle(color: Colors.white),
                                    ),
                                  )),
                            ),
                          ),
                        )),
                    Padding(
                      padding: new EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 10.0),
                      child: new Center(
                        child: new FlatButton(
                          child: new Text(
                            '提现记录',
                            style: TextStyle(fontSize: 15),
                          ),
                          textColor: Color.fromRGBO(27, 182, 251, 1),
                          //color: Color.fromRGBO(27, 182, 251, 1),
                          onPressed: () {
                            //drawrec
                            Navigator.pushNamed(context, "drawrec");
                          },
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ));
  }
}
